package com.kaifamiao.uncaught;

import java.lang.Thread.UncaughtExceptionHandler;

public class UncaughtExceptionHandlerTest {

    public static void main(String[] args) {

        // 创建未捕获异常处理器
        UncaughtExceptionHandler ueh = new UncaughtExceptionHandler() {
            @Override
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println( "thread: " + t.getName() );
                System.out.println( "exception: " + e.getMessage() );
            }
        };

        Thread t = new Thread(() ->{
            try {
                Thread.sleep(10000L);
            } catch (InterruptedException cause) {
                throw new RuntimeException("我是故意的", cause);
            }
        }, "李某芳");

        // 在线程体外部设置"未捕获异常处理器"
        t.setUncaughtExceptionHandler( ueh );

        t.start();

        t.interrupt();

    }

}
